const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    ProductId: 0,
    ProductNo: "",
    ProductName: "",
    Unit: "",
    Price: 0,
    IsView: false,
    ProductTypeId: null,
    ProductTypeName: "",
    ProviderId: null,
    ProviderName: "",
    FunctionDescription:"",
    cteV: true,
    errorText:'',
    showTopTips: false
  },
  changeText: function(e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log(res)
        var arr=[].concat(that.data.files)
        var obj={
          ProductImageId: 0,
          Title: 'image',
          Url: res.tempFilePaths[0],
          CreateTime: new Date()
        }
        arr.push(obj)
        that.setData({
          // files: that.data.files.concat(res.tempFilePaths)
          files:arr
        });
        
      }
    })
  },
  previewImage: function (e) {
    console.log(e)
    console.log(this.data.files)
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  cancelImg: function(e){
    var arr = this.data.files
    arr.splice(e.target.dataset.index, 1)
    this.setData({
      files: arr
    })
  },
  switchStop: function(e){
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
    console.log(this.data.IsView)
  },
  compReq: function(){
    var that = this
    var stop = true
    var arr = this.data.proList
    if (this.data.ProductName == '') {
      this.setData({
        showTopTips: true,
        errorText: '产品名称不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.ProductNo == '') {
      this.setData({
        showTopTips: true,
        errorText: '产品编号不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.Unit == '') {
      this.setData({
        showTopTips: true,
        errorText: '数量单位不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.ProductTypeId == null) {
      this.setData({
        showTopTips: true,
        errorText: '产品大类必选'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.ProviderId == null) {
      this.setData({
        showTopTips: true,
        errorText: '供应商必选'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.files < 1) {
      this.setData({
        showTopTips: true,
        errorText: '至少添加一张图片'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if(stop){
      wx.request({
        url: app.globalData.api + 'HouseStore/Product',
        method: "POST",
        header: {
          Authorization: app.globalData.Authorization
        },
        data: {
          ProductId: this.data.ProductId,
          ProductNo: this.data.ProductNo,
          Unit: this.data.Unit,
          Price: this.data.Price,
          IsView: this.data.IsView,
          ProductName: this.data.ProductName,
          ProductTypeId: this.data.ProductTypeId,
          ProductTypeName: this.data.ProductTypeName,
          ProviderId: this.data.ProviderId,
          ProviderName: this.data.ProviderName,
          FunctionDescription: this.data.FunctionDescription,
          ProductImages: this.data.files,
        },
        success: function (res) {
          console.log(res)
          if (res.data.Code) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            that.setData({
              showTopTips: true,
              errorText: res.data.Error
            })
            setTimeout(function () {
              that.setData({
                showTopTips: false
              }, 2000)
            })
          }
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.create == 1) {
      this.setData({
        cteV: false
      })
    }
    if (options.id){
      app.req({
        url: 'HouseStore/Product/' + options.id,
        success: function(res){
          console.log(res.data)
          var data = res.data.Result
          that.setData({
            ProductId: options.id,
            ProductNo: data.ProductNo,
            ProductName: data.ProductName,
            Unit: data.Unit,
            Price: data.Price,
            IsView: data.IsView,
            ProductTypeId: data.ProductTypeId,
            ProductTypeName: data.ProductTypeName,
            FunctionDescription: data.FunctionDescription,
            files: data.ProductImages
          })
        }
      })
    } else {
      this.setData({
        ProductId: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})